Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Switch sr-arithmetic benchmarking to criterion - #3902

Merged
bkchr merged 10 commits into
paritytech:masterfrom
expenses:sr-arithmetic-criterion
Oct 29, 2019
Merged

Switch sr-arithmetic benchmarking to criterion#3902
bkchr merged 10 commits into
paritytech:masterfrom
expenses:sr-arithmetic-criterion

Conversation

@expenses

Copy link
Copy Markdown
Contributor

As mentioned by @bkchr and @kianenigma in #3799, we should try and use criterion for benchmarking where possible. I wanted to replace all old #[bench] benchmarks with criterion in this pr, but unfortunately the subkey benchmarks aren't exposed, the node executor benchmarks require an unwanted amount of code duplication and the phragmen benchmarks are complied via a macro. I'm not sure it's worth it to try and convert these benchmarks at the moment.

I did rename the core/primitives benchmark file from benches.rs to bench.rs to be more consistent though.

#[cfg(feature = "bench")]
#[bench]
fn bench_paranoiac(b: &mut Bencher) {
b.iter(|| generate_key("polk"));
}
#[cfg(feature = "bench")]
#[bench]
fn bench_not_paranoiac(b: &mut Bencher) {
b.iter(|| generate_key("polk"));
}

#[cfg(feature = "benchmarks")]
mod benches {
use super::*;
use test::Bencher;
#[bench]
fn wasm_execute_block(b: &mut Bencher) {
let (block1, block2) = blocks();
b.iter(|| {
let mut t = new_test_ext(COMPACT_CODE, false);
WasmExecutor::new().call(&mut t, "Core_execute_block", &block1.0).unwrap();
WasmExecutor::new().call(&mut t, "Core_execute_block", &block2.0).unwrap();
});
}
}

macro_rules! phragmen_benches {
($($name:ident: $tup:expr,)*) => {
$(
#[bench]
fn $name(b: &mut Bencher) {
let (v, n, t, e, eq_iter, eq_tol) = $tup;
println!("----------------------");
println!(
"++ Benchmark: {} Validators // {} Nominators // {} Edges-per-nominator // {} \
total edges // electing {} // Equalize: {} iterations -- {} tolerance",
v, n, e, e * n, t, eq_iter, eq_tol,
);
do_phragmen(b, v, n, t, e, eq_iter, eq_tol);
}
)*
}
}

@kianenigma

Copy link
Copy Markdown
Contributor

I'm not sure it's worth it to try and convert these benchmarks at the moment.

Indeed. My personal opinion is that: criterion is great but let's not refactor to it just for the sake of refactoring. Criterion is great when you want to pass complex data to the benchmark. This is something that #[bench] cannot do for you neatly and you ned up with ugly macros like phragmen benchmarks Notably, you can see the hashing benchmarks in which we want to benchmark over first fixed size and then dynamically linear growth etc. I see it rational to switch to criterion when you want to extend some benchmarks of some crate (or you plan to do) and then you realise test crate does not cut it for you.

from this perspective,

  • the phragmen can use criterion but at the time it was way too much hassle to set up criterion properly and I just pass my desired data with a macro. Nonetheless, the macro is just to do what criterion can also do so it does need a refactor at some point.
  • arithmatic can use criterion given but noting that we also want to probably pass more dynamic and sphisticated data into the benchmark. We can further discuss this
  • node/executor seem to be fine with #[bench]
  • subkey seems to be fine with #[bench]

of course, this is my personal opinion and how I would like to spend my time. You can go ahead and do more if you want. Nonetheless, thanks for the arithmetic benchmarks that would be very helpful now or in the future! :)

Comment thread core/sr-arithmetic/benches/bench.rs Outdated

@kianenigma kianenigma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can use criterion a bit more and remove almost-duplicated-code. Regarding the rest, I would be happier to approve this earlier and not jump into refactoring every single #[bench], but the author can decide otherwise. Thanks :)

Comment thread core/sr-arithmetic/benches/bench.rs Outdated
Comment thread core/sr-arithmetic/benches/bench.rs Outdated
@kianenigma kianenigma added the A0-please_review Pull request needs code review. label Oct 24, 2019
@bkchr

bkchr commented Oct 24, 2019

Copy link
Copy Markdown
Member

The reason I want to see this change #[bench] is unstable and requires a nightly compiler and I'm not sure they will ever stabilize it. :)

Ty for the pr! :)

Comment thread core/sr-arithmetic/Cargo.toml Outdated
Comment thread core/sr-arithmetic/benches/bench.rs Outdated
expenses and others added 3 commits October 24, 2019 09:02
Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
Comment thread core/sr-arithmetic/benches/bench.rs Outdated

fn bench_subtraction(c: &mut Criterion) {
bench_op(c, "subtraction", |a, b| {
let _ = a.clone().sub(&b);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let _ = a.clone().sub(&b);
let _ = a.clone().sub(&b);

better to have let _ = .. consistently for all or none.

let a = random_big_uint(size as usize);
let b = random_big_uint(size as usize);

bencher.iter(|| op(&a, &b));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I just ran it and being amazed of why division is as fast as addition: we don't do division p / q if the size of q is equal to p. Need to be reworked. I'd leave it up to you regarding how to bake it out of criterion, but ideally, given p with size limbs, we should chose a secondary random number between [2, size-1] and build a number of that size and divide them.

@kianenigma kianenigma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a small rework of the division

@gavofyork gavofyork added A4-gotissues and removed A0-please_review Pull request needs code review. labels Oct 25, 2019
@kianenigma

Copy link
Copy Markdown
Contributor

@expenses done and review needed? please update the labels as needed :)

@expenses
expenses requested a review from kianenigma October 27, 2019 21:19
@expenses

Copy link
Copy Markdown
Contributor Author

I don't think I can set labels, sorry.

@kianenigma kianenigma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good! CI is a bit overloaded now so will merge later.

@bkchr
bkchr merged commit 23c1956 into paritytech:master Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants